<template>
  <div class="create-service">
    <el-dialog
      title="新增/修改服务"
      :visible.sync="dialogVisible"
      width="800px"
      @close="close"
    >
      <el-form ref="formRef" :model="form" :rules="formRules" label-width="130px">
        <el-form-item label="系统名称" prop="systemId">
          <el-select :disabled="disabled" v-model="form.systemId" clearable placeholder="请选择系统名称">
            <el-option
              v-for="(item, index) in systemIdList"
              :key="index"
              :label="item.systemCode + ' / ' + item.systemName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-radio v-model="form.type" :disabled="disabled" label="2">黑名单</el-radio>
          <el-radio v-model="form.type" :disabled="disabled" label="1">白名单</el-radio>
        </el-form-item>
        <el-form-item label="ip地址" prop="ip">
          <el-input v-model="form.ip" :disabled="disabled" placeholder="请输入ip地址" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" :maxlength="200" :disabled="disabled" placeholder="请输入备注" type="textarea" :rows="4" />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button :loading="!canClick" :disabled="!canClick" type="primary" @click="interfaceServiceConfirm('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addBlackAndWhiteList, updateRosterInfo } from '@/api/blackAndWhiteLists'

export default {
  name: 'AddList',
  props: {
    systemIdList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      dialogVisible: false,
      disabled: false,
      canClick: true,
      form: {},
      formRules: {
        systemId: { required: true, message: '请选择系统名称', trigger: 'blur' },
        type: { required: true, message: '请选择类型', trigger: 'blur' },
        ip: { required: true, message: '请输入ip地址', trigger: 'blur' }
      }
    }
  },
  methods: {
    close() {
      this.form = {}
      this.disabled = false
    },
    interfaceServiceConfirm() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          this.canClick = false
          if (this.form.id) {
            updateRosterInfo({
              id: this.form.id,
              systemId: this.form.systemId,
              type: this.form.type,
              ip: this.form.ip,
              remark: this.form.remark
            }).then(res => {
              if (res.code === '00000') {
                this.$message.success('修改成功')
                this.dialogVisible = false
                this.$emit('getTableData')
                this.form = {}
              }
            }).finally(() => {
              this.canClick = true
            })
          } else {
            addBlackAndWhiteList(this.form).then(res => {
              if (res.code === '00000') {
                this.$message.success('添加成功')
                this.dialogVisible = false
                this.$emit('getTableData')
                Object.assign(this.form, {
                  type: null,
                  ip: null,
                  systemId: null,
                  remark: null
                })
              }
            }).finally(() => {
              this.canClick = true
            })
          }
        }
      })
    }
  }
}
</script>
<style lang="scss">
</style>
